﻿
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace xDataSet
{
    public class ObClass
    {
        //LogEntity _log;

        private string _Model;

        /// <summary>
        /// 被调用的命名空间
        /// </summary>
        public string Model
        {
            get { return _Model; }
        }
        private string _Class;
        /// <summary>
        /// 被调用的类名称
        /// </summary>
        public string Class
        {
            get { return _Class; }
        }

        private string _Method;
        /// <summary>
        /// 被调用的方法名称
        /// </summary>
        public string Method
        {
            get { return _Method; }
        }
        /// <summary>
        /// 指定调用的版本
        /// </summary>
        private string _Version;
        public string Version
        {
            get { return _Version; }
        }
        private string _UserId;
        /// <summary>
        /// 用户的ID信息
        /// </summary>
        public string UserId
        {
            get { return _UserId; }
        }

        private string _Token;
        /// <summary>
        /// Token
        /// </summary>
        public string Token
        {
            get { return _Token; }
        }

        private bool _isCache;

        private string _Paramters;
        /// <summary>
        /// 调用的参数
        /// </summary>
        public string Paramters
        {
            get { return _Paramters; }
        }

        private string _ObjectKey;

        public string ObjectKey
        {
            get { return _Model + _Class + _Version; }
        }
        public string CacheKey
        {
            get { return _Model + _Class + _Method + _Version + RemoveExuChar(_Paramters) + RemoveExuChar(Token); }
            //get { return "XADFKHASDI"; }
        }
        public bool isCache
        {
            get { return _isCache; }
        }
        private int _ExpireTime = 0;

        /// <summary>
        /// 缓存时间（分）
        /// </summary>
        public int ExpireTime
        {
            get { return _ExpireTime; }
        }
        public ObClass(JObject ob)
        {
            
            try
            {
                //判断是否强制刷新
                if (ob["CACHE"] != null)
                {
                    if (ob["CACHE"].ToString().ToUpper().Equals("T"))
                    {
                        _isCache = true;
                    }
                    else
                    {
                        _isCache = false;
                    }
                }
                else
                {
                    _isCache = true;
                }
                //用户编号
                if (ob["USERID"] != null)
                {
                    _UserId = ob["USERID"].ToString();
                }

                if (ob["PARAM"] != null)
                {
                    _Paramters = ob["PARAM"].ToString();
                }

                if (ob["TOKEN"] != null)
                {
                    _Token = ob["TOKEN"].ToString();
                }


                if (ob["MODEL"] == null)
                {
                    //类不存在异常 抛出
                    //throw new ParamtersException(SysConst.MessageCode.ErrorCode_SpaceException +
                    //    "@@" + SysConst.MessageCode.ErrorCode_SpaceException_Describe);
                }
                else
                {
                    _Model = ob["MODEL"].ToString();
                }

                if (ob["CLASS"] == null)
                {
                    //类不存在异常 抛出
                    //throw new ParamtersException(SysConst.MessageCode.ErrorCode_ClassException +
                    //    "@@" + SysConst.MessageCode.ErrorCode_ClassException_Describe);
                }
                else
                {
                    _Class = ob["CLASS"].ToString();
                }

                if (ob["METHOD"] == null)
                {
                    //throw new ParamtersException(SysConst.MessageCode.ErrorCode_MetodException +
                    //    "@@" + SysConst.MessageCode.ErrorCode_MetodException_Describe);
                }
                else
                {
                    _Method = ob["METHOD"].ToString();
                }

                if (ob["VERSION"] == null)
                {
                    //throw new ParamtersException(SysConst.MessageCode.ErrorCode_VersionException +
                    //    "@@" + SysConst.MessageCode.ErrorCode_VersionException_Describe);
                }
                else
                {
                    _Version = ob["VERSION"].ToString();
                }
                if (ob["EXPIRETIME"] != null)
                {
                    Int32.TryParse(ob["EXPIRETIME"].ToString(), out _ExpireTime);
                }
            }
            catch (Exception ex)
            {
                //_log.AddErrorLog("ERP.WebApi.Com.ObClass.ObClass(JObject,LogEntity)", "解析请求命令失败", ex.Message);
            }
        }
        /// <summary>
        /// 移除特殊字符 : \r\n
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private string RemoveExuChar(string str)
        {
            str = str.Replace("\r\n", "");
            str = str.Replace(":", "");
            str = str.Replace("{", "");
            str = str.Replace("}", "");
            str = str.Replace(" ", "");
            str = str.Replace("\"", "");
            str = str.Replace("-", "");
            return str;
        }
    }
}